package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.system.domain.DataVisualRule;
import com.ruoyi.system.domain.VisualModule;
import com.ruoyi.system.mapper.DataVisualRuleMapper;
import com.ruoyi.system.mapper.SysUserModelMapper;
import com.ruoyi.system.service.ISysUserModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

import static com.ruoyi.system.utils.DictUtils.getCacheName;

@Service
public class SysUserModelServiceImpl implements ISysUserModelService
{
    @Autowired
    private SysUserModelMapper userModelMapper;

    @Autowired
    private DataVisualRuleMapper dataVisualRuleMapper;

    @Override
    public List<VisualModule> selectAllUserModel(VisualModule visualModule, String moduleIds) {

        //前端传modeuleId会多出一个逗号，所以要处理一下
        if(moduleIds != null && !"".equals(moduleIds) && moduleIds != ""){
            moduleIds.replace(",","");
            visualModule.setModuleId(Integer.valueOf(moduleIds));
        }

        return userModelMapper.selectAllUserModel(visualModule);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteUserModelByIds(String[] ids) {
        int i = userModelMapper.deleteUserModelByIds(ids);
        if(i>0){
            int i1 = userModelMapper.deleteUserModelRuleById(ids);
            CacheUtils.removeAll(getCacheName());
        }else{
            throw new BusinessException("删除大屏元素失败！回滚");
        }
        return i;
    }

    @Override
    public VisualModule selectUserModelById(Integer id) {
        return userModelMapper.selectUserModelById(id);
    }

    @Override
    public int updateUserModelById(VisualModule visualModule) {
        return userModelMapper.updateUserModelById(visualModule);
    }

    @Override
    public List<DataVisualRule> selectDataRuleByVisualId(Integer visualId) {
        return dataVisualRuleMapper.selectDataRuleByVisualId(visualId);
    }

    @Override
    public int updataDataRuleById(List<DataVisualRule> list) {
        return dataVisualRuleMapper.updataDataRuleById(list);
    }


}
